package net.arvin.service;

import net.arvin.dao.ResponseHandler;
import net.arvin.utils.HttpRequestClient;
import net.arvin.view.LoadingView;

import org.apache.http.Header;

import android.content.Context;
import android.widget.Toast;

import com.loopj.android.http.TextHttpResponseHandler;

public class UserService {

	public static void tryLogin(final String userName, final String password,
			final ResponseHandler handler, final Context context) {
		showLoadingDialog(context, "登陆中...");
		HttpRequestClient.startPostLoginRequest(userName, password,
				new TextHttpResponseHandler() {

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseBody, Throwable error) {
						closeLoadingDialog();
						if (responseBody == null) {
							Toast.makeText(context, "连接超时=_=!",
									Toast.LENGTH_SHORT).show();
						}
					}

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							String responseBody) {
						closeLoadingDialog();
						if (responseBody == null) {
							Toast.makeText(context, "服务器出错啦=_=!",
									Toast.LENGTH_SHORT).show();
						} else {
							if (responseBody.equals("")) {
								handler.onRequestFailed(responseBody);
							} else {
								handler.onRequsetSucceeded(responseBody);
							}
						}
					}
				}, context);
	}

	public static void tryRegister(final String user,
			final ResponseHandler handler, final Context context) {
		showLoadingDialog(context, "注册中...");
		HttpRequestClient.startPostRegisterRequest(user,
				new TextHttpResponseHandler() {

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseBody, Throwable error) {
						if (responseBody == null) {
							Toast.makeText(context, "连接超时=_=!",
									Toast.LENGTH_SHORT).show();
						}
						closeLoadingDialog();
					}

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							String responseBody) {
						if (responseBody.equals("")) {
							handler.onRequestFailed(responseBody);
						} else {
							handler.onRequsetSucceeded(responseBody);
						}
						closeLoadingDialog();
					}

				}, context);
	}

	public static void tryGetUserInfo(final String phonenumber,
			final ResponseHandler handler, final Context context) {
		HttpRequestClient.startPostGetUserInfoRequest(phonenumber,
				new TextHttpResponseHandler() {

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseBody, Throwable error) {
						if (responseBody == null) {
							Toast.makeText(context, "连接超时=_=!",
									Toast.LENGTH_SHORT).show();
						}
					}

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							String responseBody) {
						if (responseBody.equals("")) {
							handler.onRequestFailed(responseBody);
						} else {
							handler.onRequsetSucceeded(responseBody);
						}
					}

				}, context);
	}

	public static void tryResetPSW(final String phonenumber,
			final String password, final String question, final String answer,
			final ResponseHandler handler, final Context context) {
		showLoadingDialog(context, "重置密码中...");
		HttpRequestClient.startPostResetPSWRequest(phonenumber, password,
				question, answer, new TextHttpResponseHandler() {

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseBody, Throwable error) {
						if (responseBody == null) {
							Toast.makeText(context, "连接超时=_=!",
									Toast.LENGTH_SHORT).show();
						}
						closeLoadingDialog();
					}

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							String responseBody) {
						if (responseBody.equals("")) {
							handler.onRequestFailed(responseBody);
						} else {
							handler.onRequsetSucceeded(responseBody);
						}
						closeLoadingDialog();
					}

				}, context);
	}

	public static void trySaveUser(final String user,
			final ResponseHandler handler, final Context context) {
		showLoadingDialog(context, "保存中...");
		HttpRequestClient.startPostSaveUserRequest(user,
				new TextHttpResponseHandler() {
					@Override
					public void onSuccess(int statusCode, Header[] headers,
							String responseBody) {
						closeLoadingDialog();
						if (responseBody.equals("")) {
							handler.onRequestFailed(responseBody);
						} else {
							handler.onRequsetSucceeded(responseBody);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseBody, Throwable error) {
						closeLoadingDialog();
						if (responseBody == null) {
							Toast.makeText(context, "连接超时=_=!",
									Toast.LENGTH_SHORT).show();
						}
					}
				}, context);
	}

	public static void showLoadingDialog(final Context context,
			final String message) {
		LoadingView.show(context, message);
	}

	public static void closeLoadingDialog() {
		LoadingView.dismiss();
	}

}
